// 存储键值对相关indexDB库表
import { openDB } from 'idb';

// 创建或打开数据库
const dbPromise = openDB('kv-db', 1, {
    upgrade(db) {
        // 创建一个名为 'kv-store' 的对象存储，keyPath 设为 null，使其成为简单的 key-value 结构
        db.createObjectStore('kv-store');
    },
});

// 存储键值对
export async function saveValue(key, value) {
    const db = await dbPromise;
    const tx = db.transaction('kv-store', 'readwrite');
    const store = tx.objectStore('kv-store');
    // 键值对存储
    await store.put(value, key);
    await tx.done;
    console.log(`value 已存储在 key: ${key}`);
}

// 根据key读取value
export async function getValueBykey(key) {
    const db = await dbPromise;
    const tx = db.transaction('kv-store', 'readonly');
    const store = tx.objectStore('kv-store');
    // 获取指定 key 的值
    const value = await store.get(key);
    return value;
}

// 根据key删除token
export async function deleteToken(key) {
    const db = await dbPromise;
    const tx = db.transaction('kv-store', 'readwrite');
    const store = tx.objectStore('kv-store');
    // 删除指定 key 的值
    await store.delete(key);
    await tx.done;
    console.log(`Token 已删除, key: ${key}`);
}

// 获取当前登录用户ID
export async function getLoginUserId(){
    let userInfo = await getValueBykey("userInfo");
    return userInfo.userId;
}


// 在线图片列表
let pictureList = ["https://s21.ax1x.com/2024/09/09/pAmmg6e.jpg", "https://s21.ax1x.com/2024/09/09/pAmmclD.jpg",
    "https://s21.ax1x.com/2024/09/09/pAmmsfK.jpg", "https://s21.ax1x.com/2024/09/09/pAmmrY6.jpg",
    "https://s21.ax1x.com/2024/09/09/pAmm2OH.jpg","https://s21.ax1x.com/2024/09/09/pAmmWmd.jpg",
    "https://s21.ax1x.com/2024/09/09/pAmmf0A.jpg"
]

// 存储
saveValue("myPictures", pictureList)



